package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.OrderDetail;

public class OrderDetailDAO {

    /**
     * Load OrderDetail in Shop.
     * @return List OrderDetail
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public static ArrayList<OrderDetail> getList() throws Exception {
        ArrayList<OrderDetail> list = new ArrayList<OrderDetail>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<OrderDetail>) session.createCriteria(OrderDetail.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New OrderDetail
     * @param OrderDetail
     * @throws Exception
     */
    public static void add(OrderDetail orderDetail) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(orderDetail);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update OrderDetail
     * @param OrderDetail
     * @throws Exception
     */
    public static void update(OrderDetail orderDetail) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(orderDetail);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete OrderDetail
     * @param OrderDetail
     * @throws Exception
     */
    public static void delete(OrderDetail orderDetail) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(orderDetail);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find OrderDetail by ID
     * @param id
     * @return OrderDetail
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static OrderDetail find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        OrderDetail orderDetail = new OrderDetail();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            orderDetail = (OrderDetail) session.get(OrderDetail.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return orderDetail;
        }
    }

}
